package com.turing.data.scripts.yhlc.filter

import com.turing.common.util.FileUtils
import com.turing.data.engine.TextUtils
import com.turing.data.engine.utils.ScriptsUtils
import com.turing.data.vo.out.review.DependParamsVO
import com.turing.data.vo.out.review.TaskElementVO
import org.apache.commons.lang.SerializationUtils
import org.springframework.util.CollectionUtils
import com.turing.data.engine.utils.RegexExtract

/**
 * @description: 银行理财 - 日期
 * @author: mazhishen
 */
class Date_30249_46_filter {

    static List< TaskElementVO.DrawResultVO > doConvert(DependParamsVO dependParams){

        def REGEX_BACKUP = "应在"
        def REGEX_BACKUP_1 = "于"
        def REGEX_BACKUP_2 = "缴款截止时间"
        def REGEX_BACKUP_3 = "确认"

        List< TaskElementVO.DrawResultVO > trueValueSet = new ArrayList<>()
        List<TaskElementVO.DrawResultVO > resultVOs = ScriptsUtils.tryInitResult(dependParams)
        def set = new HashSet()
        if (resultVOs!=null && resultVOs.size()>0) {
            for (int i = 0; i < resultVOs.size(); i++) {
                def drawResultVO = resultVOs.get(i)
                println("Date_drawResultVO="+drawResultVO)
                def childTaskSn = drawResultVO.childTaskSn
//                if(null == childTaskSn || childTaskSn == ""){
//                    return resultVOs
//                }
                def start = drawResultVO.start
                def end = drawResultVO.end
                def text = drawResultVO.text

                drawResultVO.text = text.replaceAll("_","")
                                        .replaceAll("/","")
                                        .replaceAll("-","")
                drawResultVO.businessValue = drawResultVO.text

                        text = text.replaceAll("\n","")
                def textPathMap = dependParams.textPathMap
                def rawPath = textPathMap.get(drawResultVO.childTaskSn)
                def rawText = FileUtils.loadJson(rawPath)
                def expandOffset = 0
                //往前10个字符搜索
                while( expandOffset < 10 && start > expandOffset ){
                    def expandString = rawText.substring(start-expandOffset,start)
                    if( expandString =~ REGEX_BACKUP || expandString =~ REGEX_BACKUP_1 ||
                        expandString =~ REGEX_BACKUP_2 || expandString =~ REGEX_BACKUP_3)
                    {
                        trueValueSet.add(drawResultVO)
                        set.add(drawResultVO.text)
                        return trueValueSet
                    }
                    expandOffset ++
                }
            }
        }


        try{
            println("Date_trueValueSet1="+trueValueSet)
            if(trueValueSet.size()==0){
                String REGEX = /((于|千|干).{0,5}|确认.{0,3})20.{1,10}日|于.{1,4}年.{1,2}月.{1,2}(日|且)/
                println dependParams.JSONTextMap.size()
                println("Date_JSONTextMap="+dependParams.JSONTextMap)
                dependParams.JSONTextMap.forEach(
                        { childTaskSn, jsonText ->
                            RegexExtract.doRuleExtract(REGEX,childTaskSn, dependParams, jsonText, trueValueSet)
                        }
                )
                println("Date_trueValueSet2="+trueValueSet)
                if(trueValueSet.size()>0){
                    def textPathMap = dependParams.textPathMap
                    def childTaskSn = trueValueSet[0].childTaskSn
                    def rawPath = textPathMap.get(childTaskSn)
                    def rawText = FileUtils.loadJson(rawPath)
                    def JSONTextMap = dependParams.JSONTextMap
                    def fileTextStr = ""
                    fileTextStr = JSONTextMap.get(childTaskSn)
                    def text = trueValueSet[0].text

                    def startOffset = text.indexOf("20")
                    def endOffset = 0
                    TaskElementVO.DrawResultVO objectNew = (TaskElementVO.DrawResultVO) SerializationUtils.clone(trueValueSet[0])
                    objectNew = TextUtils.correctPositionInfoByOffset(objectNew, startOffset, endOffset, fileTextStr, rawText)

                    objectNew.text = objectNew.text.replaceAll("且","日")
                    objectNew.businessValue = objectNew.text

                    trueValueSet.clear()
                    trueValueSet.add(objectNew)
                    return trueValueSet
                }
            }
        }
        catch (Exception e){
            e.printStackTrace()
        }

        if(!CollectionUtils.isEmpty(resultVOs)){
            trueValueSet.add(resultVOs[0])
            return  trueValueSet
        }

        TaskElementVO.DrawResultVO defaultDrawResultVO = new TaskElementVO.DrawResultVO()
        defaultDrawResultVO.text = ""
        trueValueSet.add(defaultDrawResultVO)
        return  trueValueSet

    }

}
